package superbird;

import java.awt.image.BufferedImage;
import java.io.IOException;

import javax.imageio.ImageIO;

/**
 *小鸟类
 */
public class Bird {

	int x, y;
	BufferedImage image;

	//size?
	int size;

	int width, height; // 宽和高度
	BufferedImage[] images;
	int index; // 下角标

	double s; // 位移量
	double time; // 事件
	double g; // 重力加速度
	double v0; // 初始速度
	double speed; // 当前速度

	double rad;

	public Bird() throws IOException {
		x = 140;
		y = 245; // 定义xy 坐标
		size = 30;
		index = 0;

		v0 = 20;
		g = 4;
		time = 0.25;
		speed = v0;

		try {
			image = ImageIO.read(getClass().getResource("0.png"));
		} catch (IOException e) {
			e.printStackTrace();
		}

		width = image.getWidth();
		height = image.getHeight();
//		width = 200;
//		height = 100;

		images = new BufferedImage[8];
		for (int i = 0; i < 8; i++) {
			images[i] = ImageIO.read(getClass().getResource(i + ".png"));
		}

	}
	//小鸟图片的切换
	public void fly() {
		index++;
		image = images[(index / 8) % 8];//8/8=1 9/8 10/8?  循环？
	}
	//小鸟跳动一下
	public void step() {

		double v = speed;
		//s= v0*t + gt^2/2
		s = v * time - g * time * time;
		y = (int) (y - s);
		speed = v - g * time;

		//改变小鸟的角度变化速度

		rad = Math.atan(s / 15);// 让小鸟有角度的抛起来,伴随着位移的变化，去改变角度（所以用s/）参数值越大角度越大

	}
	//小鸟向上飞
	public void flyUp() {
		speed = v0;
	}

	public boolean hit(Column column) {// 碰撞柱子

		//?
		if (x > column.x - size / 2 - column.width / 2 && x < column.x + column.width / 2 + size / 2) {

			if (y > column.y - column.gap / 5  && y < column.y + column.gap / 2 ) {
				return false;
			}
			return true;// 将此处改为false，即不死鸟
		}

		return false;
	}

	public boolean hit(Ground ground) {// 碰撞地板

		if (y >= ground.y - size / 2) {
			rad = -Math.PI / 2;
			return true;

		}
		return false;
	}

}
